package com.itheima.d2_reflect_test;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;

/*
    对象框架
    目标：保存任意对象的字段和其数据到文件中去
 */
public class ObjectFrame {
    public static void getPer(Object obje) throws Exception {
        BufferedWriter bw = new BufferedWriter(new FileWriter("user.txt", true));
        Class obj = obje.getClass();
        bw.write("=====================" + obj.getSimpleName() + "==========================");
        bw.newLine();
        bw.flush();
        Field[] df = obj.getDeclaredFields();//获取所有class的成员变量
        for (Field field : df) {
            field.setAccessible(true);
            String name = field.getName();//获得成员变量的名称
            Object o = field.get(obje);//获取成员变量的值
            bw.write(name + "=" + o);
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }
}
